package com.nengjiang.wsplayer.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.nengjiang.wsplayer.Config;
import com.nengjiang.wsplayer.R;
import com.nengjiang.wsplayer.oksocket.OkManager;
import com.nengjiang.wsplayer.utils.SPConfig;
import com.nengjiang.wsplayer.utils.SPUtils;

public class ConfigUrlActivity extends AppCompatActivity {

    private EditText et_socket_url;
    private EditText et_socket_port;
    private EditText et_rtsp_url;
    private Button bt_save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config_url);
        initView();
    }

    private void initView() {
        et_socket_url = findViewById(R.id.et_socket_url);
        et_socket_port = findViewById(R.id.et_socket_port);
        et_rtsp_url = findViewById(R.id.et_rtsp_url);
        bt_save = findViewById(R.id.bt_save);
        et_socket_url.setText(SPUtils.getInstance().getString(SPConfig.SP_KEY_SOCKET_URL));
        et_socket_port.setText(SPUtils.getInstance().getInt(SPConfig.SP_KEY_SOCKET_PORT)==-1?"":SPUtils.getInstance().getInt(SPConfig.SP_KEY_SOCKET_PORT)+"");
        et_rtsp_url.setText(SPUtils.getInstance().getString(SPConfig.SP_KEY_RTSP_PUSH_URL));

        bt_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveUrl();
            }
        });
    }

    private void saveUrl() {
        String socketUrl = et_socket_url.getText().toString().trim();
        String socketPort = et_socket_port.getText().toString().trim();
        String rtspUrl = et_rtsp_url.getText().toString().trim();

        Config.SOCKET_URL = socketUrl;
        Config.SOCKET_PORT = Integer.valueOf(socketPort).intValue();
        Config.RTSP_PUSH_URL = rtspUrl;


        if (!TextUtils.isEmpty(socketUrl)) {
            SPUtils.getInstance().put(SPConfig.SP_KEY_SOCKET_URL, socketUrl);
        }

        if (!TextUtils.isEmpty(socketPort)) {
            SPUtils.getInstance().put(SPConfig.SP_KEY_SOCKET_PORT, Integer.valueOf(socketPort).intValue());
        }

        if (!TextUtils.isEmpty(rtspUrl)) {
            SPUtils.getInstance().put(SPConfig.SP_KEY_RTSP_PUSH_URL, rtspUrl);
        }
        if(OkManager.getInstance().isConnecting()){
        }
        OkManager.getInstance().updateUrl();
        finish();
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        Log.e("demo2", event.getAction() + "," + event.getKeyCode());
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
                View rootView = getWindow().getDecorView();
                View currentFocus = rootView.findFocus();
                Log.e("demo2", "id:" + currentFocus.getId());
                Log.e("demo2", "isShowing:" + isSoftShowing());
                if(currentFocus.getId() == R.id.et_socket_url){
                    if(isSoftShowing() == false) {
                        showOrHide(this);
                        et_socket_port.requestFocus();
                    }
                    return true;
                }
                if (currentFocus.getId() == R.id.et_socket_port) {
                    if(isSoftShowing() == false) {
                        showOrHide(this);
                        et_rtsp_url.requestFocus();
                    }
                    return true;
                }
                if (currentFocus.getId() == R.id.et_rtsp_url) {
                    if(isSoftShowing() == false) {
                        bt_save.requestFocus();
                    }
                    return true;
                }
            }

            if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
                View rootView = getWindow().getDecorView();
                View currentFocus = rootView.findFocus();
                Log.e("demo2", "id:" + currentFocus.getId());
                Log.e("demo2", "isShowing:" + isSoftShowing());
                if (currentFocus.getId() == R.id.et_socket_port) {
                    if(isSoftShowing() == false) {
                        showOrHide(this);
                        et_socket_url.requestFocus();
                    }
                    return true;
                }
                if (currentFocus.getId() == R.id.et_rtsp_url) {
                    if(isSoftShowing() == false) {
                        showOrHide(this);
                        et_socket_port.requestFocus();
                    }
                    return true;
                }

                if(currentFocus.getId() == R.id.bt_save){
                    if(isSoftShowing() == false) {
                        showOrHide(this);
                        et_rtsp_url.requestFocus();
                    }
                    return true;
                }
            }


        }
        return super.dispatchKeyEvent(event);
    }

    //如果输入法在窗口上已经显示，则隐藏，反之则显示
    public static void showOrHide(Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    //view为接受软键盘输入的视图，SHOW_FORCED表示强制显示
    public static void showOrHide(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        //  imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);//SHOW_FORCED表示强制显示
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
    }

    //调用隐藏系统默认的输入法
    public static void showOrHide(Context context, Activity activity) {
        ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
    }

    //获取输入法打开的状态
    private boolean isSoftShowing() {
        //获取当屏幕内容的高度
        int screenHeight = this.getWindow().getDecorView().getHeight();
        //获取View可见区域的bottom
        Rect rect = new Rect();
        //DecorView即为activity的顶级view
        this.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        //考虑到虚拟导航栏的情况（虚拟导航栏情况下：screenHeight = rect.bottom + 虚拟导航栏高度）
        //选取screenHeight*2/3进行判断
        Log.i("demo2","screenHigh: " + screenHeight + " rectViewBom : " + rect.bottom );
        return screenHeight * 2 / 3 > rect.bottom;
    }
}